//! \file QueueList_TODO.h
//! \brief Defines the <code>fullsail_ai::data_structures::QueueList</code> class template
//! implementation.
//! \author Cromwell D. Enage
#ifndef _FULLSAIL_AI_FUNDAMENTALS_QUEUELIST_TODO_H_
#define _FULLSAIL_AI_FUNDAMENTALS_QUEUELIST_TODO_H_

namespace fullsail_ai { namespace fundamentals {

	//! \todo
	//!   - Complete the definition of the <code>QueueList::Iterator</code> pre-increment operator.
	//!   - Complete the definition of the <code>QueueList::Iterator</code> dereference operator.
	//!   - Define the <code>QueueList</code> constructor.
	//!   - Define the <code>QueueList</code> destructor.
	//!   - Define the <code>QueueList</code> methods.
	// constructor
	template <typename T>
	QueueList<T>::QueueList()
	{
		front = NULL;
		back = NULL;
	}
	// destructor
	template <typename T>
	QueueList<T>::~QueueList()
	{
		removeAll();
	}
	
	
	template <typename T>
	bool QueueList<T>::isEmpty() const
	{
		if(front == NULL)
			return true;
		return false;
	}

	template <typename T>
	void QueueList<T>::enqueue(T element)
	{
		Node* newNode = new Node(element);
		if(isEmpty())
		{
			front = newNode;
			back = front;
		}
		else
		{
			back->next = newNode;
			back = newNode;
		}
	}

	template <typename T>
	T QueueList<T>::getFront() const
	{
		return front->data;
	}

	template <typename T>
	void QueueList<T>::dequeue()
	{
		// remove from the front
		if(front == back)
		{
			front = back = NULL;
			return;
		}
		front = front->next;
	}

	template <typename T>
	void QueueList<T>::removeAll()
	{
		while(front != NULL)
		{
			Node* temp = front;
			front = front->next;
			delete temp;
		}
	}

	template <typename T>
	bool QueueList<T>::contains(T element) const
	{
		Node* temp = front;
		while(temp != NULL)
		{
			if(temp->data == element)
				return true;
			temp = temp->next;
		}
		return false;
	}

	template <typename T>
	void QueueList<T>::remove(T element)
	{
		Node* current = front, *prev = front;
		while(current != NULL)
		{
			if(current->data == element)
			{
				// remove this node then return
				// if its the first node
				if(current == front)
				{
					front = front->next;
					delete current;
				}
				// if its last
				else if(current == back)
				{
					back = prev;
					delete current;
					back->next = NULL;
				}
				// if its the only one
				else if(front == back)
				{
					delete current;
					front = back = NULL;
				}
				// else its in the middle
				else
				{
					prev->next = current->next;
					delete current;
				}
				return;
			}
			// else we move forward
			prev = current;
			current = current->next;	
		}
	}

	template <typename T>
	typename QueueList<T>::Iterator& QueueList<T>::Iterator::operator++()
	{
		// TODO: Complete the definition of this operator.
		if(currentNode->next == NULL)
			currentNode = NULL;
		else
			currentNode = currentNode->next;
		return *this;
	}

	template <typename T>
	T QueueList<T>::Iterator::operator*() const
	{
		// TODO: Complete the definition of this operator.
		return currentNode->data;
	}


}}  // namespace fullsail_ai::fundamentals

#endif  // _FULLSAIL_AI_FUNDAMENTALS_QUEUELIST_TODO_H_

